<?php

session_start();

$ip = $_SERVER['REMOTE_ADDR'];

$user_email = $_SESSION['user_email'];

header( 'Content-Type: text/html; charset=utf-8' );

require_once ("../conf/conf.php");
require ("../include/constants.php");
require ("../include/function.php");

$action = $_GET['action'];

echo "IP: ". $ip .'</br>';

if($user_email == 'matei.nick@gmail.com'){
    $connection = db_connect(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);

    if($action == '1_5'){
        //echo "1.3 already installed";
        echo upgrade1_5();
    } else {
		echo userListSql();

        echo "<div style='height: 50px;'></div>";
        //echo sql2HTMLTable("SHOW CREATE TABLE keywords");
        echo sql2HTMLTable("select users.email, keywords.keywords, keywords.style from keywords join users on (users.id = keywords.user_id) ORDER BY users.id");

        echo "<div style='height: 50px;'></div>";
        //echo sql2HTMLTable("SHOW CREATE TABLE verse_comments");
        echo sql2HTMLTable("select users.email, verse_comments.* from verse_comments join users on (users.id = verse_comments.user_id) ORDER BY users.id");
        
		echo "<div style='height: 50px;'></div>";
        echo sql2HTMLTable("select users.email, user_chapters_theme.* from user_chapters_theme join users on (users.id = user_chapters_theme.user_id) ORDER BY users.id");
    }

	db_disconnect($connection);
} else {
	echo 'No access' . $user_email;
}

// ====================================================
function upgrade1_5(){
    $response = '';

//    $sql = '
//    CREATE TABLE IF NOT EXISTS user_chapters_theme(
//	id       INT             AUTO_INCREMENT PRIMARY KEY,
//	theme    varchar(255) NOT NULL,
//	user_id  INT(6),
//	chapter_id  INT(6),
//	KEY user_chapters_themes_id_idx (user_id),
//	CONSTRAINT user_ch_themes_user_id_ibfk_1 FOREIGN KEY (user_id) REFERENCES users (id),
//	KEY user_ch_themes_chapter_id_idx (chapter_id),
//	CONSTRAINT user_ch_themes_chapter_id_ibfk_1 FOREIGN KEY (chapter_id) REFERENCES chapter (id)
//) ENGINE=INNODB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
//    ';
//    $response .= executeSqlUpdate($sql, "Create table user_chapters_theme");

//    $sql = 'UPDATE verse SET text = \'Nimic din ce va fi blestemat ca să fie nimicit cu desăvârşire, să nu se lipească de mâna ta, pentru ca Domnul să Se întoarcă din iuţimea mâniei Lui, să Se îndure de tine, să te ierte, şi să te înmulţească, după cum a jurat lucrul acesta părinţilor tăi,\' WHERE id = 5290';
//	$response .= executeSqlUpdate($sql, "Deuteronomul 13.17");

    return $response;
}

function executeSqlUpdate($sql, $msg){
	$r = do_query($sql);
    if($r){
        $msg .= " - OK<br>";
    } else {
        $msg .= " - ERROR<br>";
    }
    return $msg;
};

function userListSql(){
    echo sql2HTMLTable("select id, email, status, activation_date from users");
}

?>